/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Rectangle {
    id: messageswindow
    width: 300
    height: 300
    color: "#abd0ec"
    clip: true
    border.color: "#000000"
    z: 999
    visible:  false

    property string datacloudurl: ""
    property string karpoid: ""
    property string password: ""

    function show()
    {
        messagesmodel.clear()
        messageswindow.visible = !messageswindow.visible
    }

    function getmessagescb(response)
    {
        console.log("getmessagescb: " + JSON.stringify(response))

        messagesmodel.clear()

        var newmessagesread = (response.condition == "NEW" || response.condition == "ALL")

        for (var m = 0; m < response.messagelist.length; m++)
        {
            messagesmodel.append(response.messagelist[m])
        }


        messagebox.visible = false

        if (newmessagesread == true)
        {
            // the new messages are read and the letter icon should be changed
            DCDATA.notifynewmessages(false)
        }
    }



    MessageBox
    {
        id: messagebox
    }


    Rectangle {
        id: buttonsrect
        height: 30
        color: "#3d75a0"
        border.color: "#000000"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        PushButton {
            id: newmessagesbutton
            width: 80
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 4
            label: "New msgs..."
            onButtonpressed:
            {
                messagebox.text = "Get the new messages..."
                messagebox.show("")

                var messagelist = new DCDATA.DCMessageList()
                messagelist.karpoid = karpoid
                messagelist.password = password
                messagelist.condition = "NEW"
                DCDATA.send(messagelist, datacloudurl, "getmessages", getmessagescb)
            }
        }

        PushButton {
            id: allmessagesbutton
            width: 80
            anchors.left: newmessagesbutton.right
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 4
            label: "All msgs..."
            onButtonpressed:
            {
                messagebox.text = "Get all messages..."
                messagebox.show("")
                var messagelist = new DCDATA.DCMessageList()
                messagelist.karpoid = karpoid
                messagelist.password = password
                messagelist.condition = "ALL"
                DCDATA.send(messagelist, datacloudurl, "getmessages", getmessagescb)
            }
        }

        PushButton {
            id: sentmessagesbutton
            width: 80
            anchors.left: allmessagesbutton.right
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 4
            label: "Sent msgs..."
            onButtonpressed:
            {
                messagebox.text = "Get sent messages..."
                messagebox.show("")
                var messagelist = new DCDATA.DCMessageList()
                messagelist.karpoid = karpoid
                messagelist.password = password
                messagelist.condition = "SENT"
                DCDATA.send(messagelist, datacloudurl, "getmessages", getmessagescb)
            }
        }

        PushButton {
            id: closebutton
            x: 239
            anchors.top: parent.top
            anchors.topMargin: 4
            anchors.right: parent.right
            anchors.rightMargin: 0
            label: "Close"

            onButtonpressed:
            {
                messageswindow.visible = false
            }
        }
    }

    ListModel
    {
        id: messagesmodel

    }

    Component {
        id: messageDelegate



        Item {
            width: parent.width; height: 40
            clip: true

            Column {

                Row
                {
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    Text { text: '<b>From: </b>' + senderusername; font.pixelSize: 11;  width:  110}
                    Text { text: '<b>To: </b>' + receiverusername; font.pixelSize: 11;   width:  110}
                    Text { text: '<b>Time: </b>' + statuschangetime; font.pixelSize: 11 }
                }
                Text { text: message; font.pixelSize: 12; anchors.left: parent.left; anchors.leftMargin: 5}
            }

            MouseArea
            {
                anchors.fill: parent

                onClicked:
                {
                    messagesview.currentIndex = index
                }

                onDoubleClicked:
                {
                    showmessage.messagefrom = "<b>From: </b>" + messagesmodel.get(index).senderusername
                    showmessage.messageto = "<b>To: </b>" + messagesmodel.get(index).receiverusername
                    showmessage.messagetime = "<b>Time: </b>" + messagesmodel.get(index).statuschangetime
                    showmessage.messagecontent = messagesmodel.get(index).message;

                    showmessage.visible = true
                }
            }
        }
    }

    ListView {
        id: messagesview
        clip: true
        anchors.top: buttonsrect.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.topMargin: 0

        boundsBehavior: Flickable.StopAtBounds

        model: messagesmodel
        delegate: messageDelegate

        //highlightMoveSpeed: -1 // immediately, no transition
        highlightMoveDuration: 100
        highlight: Rectangle { color: "lightyellow"; radius:  3}
        highlightFollowsCurrentItem: true
        focus: true


    }


    Rectangle
    {
        id: showmessage
        visible: false
        anchors.fill: parent

        property alias messagecontent: messagetext.text
        property alias messagefrom: fromtext.text
        property alias messageto: totext.text
        property alias messagetime: timetext.text

        PushButton {
            id: closeshowmessagebutton
            width: 110
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10
            label: "Close"
            onButtonpressed:
            {
                showmessage.visible = false
            }
        }



        Row
        {
            id: msgheader
            height: 20
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10


            Text
            {
                id: fromtext
                font.pixelSize: 11
                width: 110

            }
            Text
            {
                id: totext
                font.pixelSize: 11
                width: 110

            }
            Text
            {
                id: timetext
                font.pixelSize: 11
            }
        }

        Text
        {
            id: messagetext
            font.pixelSize: 12
            wrapMode: Text.Wrap
            anchors.top: msgheader.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.bottom: closeshowmessagebutton.top
            anchors.bottomMargin: 10

        }

    }

}
